﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GoShopping.BLL;
using GoShopping.Utility;
using GoShopping.Model.Enumerations;

public partial class Member_MyOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetDDLOrderStatus();
            GoShopping.BLL.Orders.OrderBean ob = new GoShopping.BLL.Orders.OrderBean();
            if (Request.QueryString["status"] != null)
            {
                GetDatasource(Request.QueryString["status"]);
                return;
            }
            GetOrderList(ob.GetUsersOrder(Context.User.Identity.Name));
        }
    }

    private void SetDDLOrderStatus()
    {
        ListItem li0 = new ListItem("所有交易", "0");
        ListItem li1 = new ListItem("进行中的交易", "1");
        ListItem li2 = new ListItem("成功的交易", "2");
        ListItem li3 = new ListItem("取消的交易", "3");
        ListItem li4 = new ListItem("其他", "4");
        ddlOrderStatus.Items.Add(li0);
        ddlOrderStatus.Items.Add(li1);
        ddlOrderStatus.Items.Add(li2);
        ddlOrderStatus.Items.Add(li3);
        ddlOrderStatus.Items.Add(li4);
        ListItem li5 = ddlOrderStatus.Items.FindByValue(Request.QueryString["status"]);
        int index = ddlOrderStatus.Items.IndexOf(li5);
        ddlOrderStatus.SelectedIndex = index;
        //IList<GoShopping.Utility.EnumBoundings.ComboBoxItem> cbiList = GoShopping.Utility.EnumBoundings.OptionEnum.GetEnumNameValueKeys(GoShopping.Model.Enumerations.OrderStatusType.PayedFindGoods.GetType());
        //ddlOrderStatus.DataTextField = "Name";
        //ddlOrderStatus.DataValueField = "Value";
        //ddlOrderStatus.DataSource = cbiList;
        //ddlOrderStatus.DataBind();
    }
    private void GetOrderList(object dataSource)
    {
        //GoShopping.BLL.UserBean ub = new GoShopping.BLL.UserBean();
        //GoShopping.BLL.Orders.OrderBean ob = new GoShopping.BLL.Orders.OrderBean();
        //GoShopping.Model.UserInfo ui = ub.GetOneUser(Context.User.Identity.Name);
        //IList<GoShopping.Model.Order> io = ob.GetUsersOrder(ui.ID);
        lvOrdered.DataSource = dataSource;
        lvOrdered.DataBind();
    }
    protected void lvOrdered_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "btnRemove")
        {
            Response.Redirect("OrderCancel.aspx?orderId=" + e.CommandArgument);
        }
        else if (e.CommandName == "btnDetail")
        {
            Response.Redirect("OrderDetail.aspx?orderId=" + e.CommandArgument);
        }
    }
    protected void lvOrdered_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        ((DataPager)(this.lvOrdered.FindControl("DataPager1"))).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        GetDatasource(ddlOrderStatus.SelectedValue);
    }
    protected void lvOrdered_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewItem lvi = e.Item;
            Label lblState = lvi.FindControl("lblState") as Label;
            LinkButton btnRemove = lvi.FindControl("btnCancel") as LinkButton;
            btnRemove.Enabled = false;
            GoShopping.Model.Enumerations.OrderStatusType ost = (GoShopping.Model.Enumerations.OrderStatusType)Enum.Parse(typeof(GoShopping.Model.Enumerations.OrderStatusType), lblState.Text);
            switch (ost)
            {
                case GoShopping.Model.Enumerations.OrderStatusType.PayedFindGoods:
                    {
                        lblState.Text="配货中";
                        btnRemove.Enabled = true;
                    } break;
                case GoShopping.Model.Enumerations.OrderStatusType.SendedWaitRecieve:
                case GoShopping.Model.Enumerations.OrderStatusType.WaitForPay:
                    {
                        lblState.Text = "已发货";
                    } break;
                case GoShopping.Model.Enumerations.OrderStatusType.Success:
                    {
                        lblState.Text =  "交易成功";
                    } break;
                case GoShopping.Model.Enumerations.OrderStatusType.Canceled:
                    {
                        lblState.Text = "订单已经取消";
                    } break;
                case GoShopping.Model.Enumerations.OrderStatusType.Returning:
                    {
                       lblState.Text = "退货中";
                    } break;
                case GoShopping.Model.Enumerations.OrderStatusType.ReturnPrdt:
                    {
                       lblState.Text = "退货成功";
                    } break;
                case GoShopping.Model.Enumerations.OrderStatusType.ReturningCash:
                    {
                        lblState.Text = "退款中";
                    } break;
                default: break;
            }
        }
    }
    protected void ddlOrderStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetDatasource(ddlOrderStatus.SelectedValue);
    }

    private void GetDatasource(string value)
    {
        GoShopping.BLL.Orders.OrderBean ob = new GoShopping.BLL.Orders.OrderBean();
        switch (value)
        {
            case "0":
                {
                    GetOrderList(ob.GetUsersOrder(Context.User.Identity.Name));
                } break;
            case "1":
                {
                    string[] strStatus = new string[] { OrderStatusType.PayedFindGoods.ToString(), OrderStatusType.SendedWaitRecieve.ToString(), OrderStatusType.WaitForPay.ToString() };
                    GetOrderList(ob.GetOrderbyStatus(strStatus));
                } break;
            case "2":
                {
                    string[] strStatus = new string[] { OrderStatusType.Success.ToString() };
                    GetOrderList(ob.GetOrderbyStatus(strStatus));
                } break;
            case "3":
                {
                    string[] strStatus = new string[] { OrderStatusType.Canceled.ToString() };
                    GetOrderList(ob.GetOrderbyStatus(strStatus));
                } break;
            case "4":
                {
                    string[] strStatus = new string[] { OrderStatusType.Returning.ToString(), OrderStatusType.ReturnPrdt.ToString(), OrderStatusType.ReturningCash.ToString() };
                    GetOrderList(ob.GetOrderbyStatus(strStatus));
                } break;
            default: break;
        }
    }
}
